using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class MainMenuUI : MonoBehaviour
{
    [Header("UI面板")]
    [SerializeField] private GameObject mainMenuPanel;  // 主菜单面板
    [SerializeField] private GameObject roomListPanel;  // 房间列表面板
    [SerializeField] private GameObject roomPanel;      // 房间面板
    [SerializeField] private GameObject optionsPanel;   // 选项面板
    
    [Header("主菜单按钮")]
    [SerializeField] private Button playButton;      // 开始游戏按钮
    [SerializeField] private Button optionsButton;   // 选项按钮
    [SerializeField] private Button quitButton;      // 退出按钮
    
    // 当前激活的面板
    private GameObject currentPanel;
    
    private void Start()
    {
        // 初始化UI状态
        currentPanel = mainMenuPanel;
        SwitchToPanel(mainMenuPanel);
        
        // 设置按钮监听
        if (playButton != null)
            playButton.onClick.AddListener(OnPlayButtonClicked);
        
        if (optionsButton != null)
            optionsButton.onClick.AddListener(OnOptionsButtonClicked);
        
        if (quitButton != null)
            quitButton.onClick.AddListener(OnQuitButtonClicked);
        
        // RoomManager相关代码已移除
        Debug.Log("房间管理系统已移除，将稍后重新实现");
        
        // 设置面板显示/隐藏事件
        SetupPanelEvents();
        
        // 确保初始状态下所有其他面板都是关闭的
        if (roomListPanel != null && roomListPanel != currentPanel)
            roomListPanel.SetActive(false);
        
        if (roomPanel != null && roomPanel != currentPanel)
            roomPanel.SetActive(false);
        
        if (optionsPanel != null && optionsPanel != currentPanel)
            optionsPanel.SetActive(false);
        
        // 创建一个字典来跟踪所有面板
        Dictionary<string, GameObject> allPanels = new Dictionary<string, GameObject>();
        if (mainMenuPanel) allPanels.Add("MainMenu", mainMenuPanel);
        if (roomListPanel) allPanels.Add("RoomList", roomListPanel);
        if (roomPanel) allPanels.Add("Room", roomPanel);
        if (optionsPanel) allPanels.Add("Options", optionsPanel);
        
        // 记录面板状态
        foreach (var panel in allPanels)
        {
            Debug.Log($"初始面板状态: {panel.Key} - {(panel.Value.activeSelf ? "激活" : "非激活")}");
        }
    }
    
    // 设置面板事件
    private void SetupPanelEvents()
    {
        
        // 选项面板可以添加关闭按钮，点击时返回主菜单
        if (optionsPanel != null)
        {
            Button backButton = optionsPanel.GetComponentInChildren<Button>();
            if (backButton != null)
            {
                backButton.onClick.AddListener(() => {
                    SwitchToPanel(mainMenuPanel);
                });
            }
        }
    }
    
    
    // 新增：处理离开房间事件的专用方法
    private void HandleLeftRoom()
    {
        Debug.Log("房间系统已移除，HandleLeftRoom是空操作");
    }
    
    // 切换到指定面板
    private void SwitchToPanel(GameObject panel)
    {
        if (panel == null)
        {
            Debug.LogError("尝试切换到空面板!");
            return;
        }
        
        Debug.Log($"切换到面板: {panel.name}");
        
        // 关闭当前面板
        if (currentPanel != null)
            currentPanel.SetActive(false);
        
        // 激活新面板
        panel.SetActive(true);
        currentPanel = panel;
        
    }
    
    // 开始游戏按钮点击事件
    private void OnPlayButtonClicked()
    {
        SwitchToPanel(roomListPanel);
    }
    
    // 选项按钮点击事件
    private void OnOptionsButtonClicked()
    {
        SwitchToPanel(optionsPanel);
    }
    
    // 退出按钮点击事件
    private void OnQuitButtonClicked()
    {
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #else
        Application.Quit();
        #endif
    }
} 